package com.example.erp.utils;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import io.jsonwebtoken.security.Keys;

import java.nio.charset.StandardCharsets;
import java.security.Key;
import java.util.Date;
import java.util.Map;

public class JwtUtils {
    private static final String secretString = "1ab07624-de5f-484d-8cc8-8bc958e02c17";
    private static final Long expire = 43200000L;

    public static String generateJwt(Map<String, Object> claims) {
        Key key = Keys.hmacShaKeyFor(secretString.getBytes(StandardCharsets.UTF_8));
        return Jwts.builder()
                .setClaims(claims)
                .signWith(key, SignatureAlgorithm.HS256)
                .setExpiration(new Date(System.currentTimeMillis() + expire))
                .compact();
    }

    public static Claims parseJwt(String json) {
        return Jwts.parser()
                .setSigningKey(Keys.hmacShaKeyFor(secretString.getBytes(StandardCharsets.UTF_8)))
                .parseClaimsJws(json)
                .getBody();
    }
}
